import program from 'commander';
import { VERSION } from './utils/constants';
import main from './index';

let actionMap = {
    install: {
        alias: 'i',
        description: 'install template',
        examples: [
            'zf-cli i',
            'zf-cli install'
        ]
    },
    config: {
        alias: 'c',
        description: 'config .zfclirc',
        examples: [
            'zf-cli config set <k> <v>',
            'zf-cli config get <k>',
            'zf-cli config remove <k>'
        ]
    },
    '*': {
        alias: '',
        description: 'not found',
        examples: []
    }
};

Object.keys(actionMap).forEach(action=>{
    program.command(action)
        .description(actionMap[action].description)
        .alias(actionMap[action].alias)
        .action(()=>{
            // console.log(action)
            if (action === 'config') {
                //实现更改配置文件
                console.log('actionName===', action);
                console.log('process.argv==',process.argv)//数组
                main(action, process.argv.slice(3));
            } else if(action === 'install') {
                
            } else {
                console.log(actionMap[action].description)
            }
            // main()
        });
});

function help() {
    console.log('\r\n '+'how to use command');
    Object.keys(actionMap).forEach(action=>{
        actionMap[action].examples.forEach(example=>{
            console.log('   - '+example);
        });
    });
}

program.on('-h', help);
program.on('--help', help);
program.version(VERSION, '-v --version').parse(process.argv);
